package com.lanyou.esb.cook.proxy.redis.push.impl;

import org.quartz.CronTrigger;
import org.quartz.Job;
import org.quartz.JobBuilder;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Scheduler;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.triggers.CronTriggerImpl;

import com.lanyou.esb.cook.proxy.common.ParamFactory;
import com.lanyou.esb.cook.proxy.redis.push.PushRedis;
import com.lanyou.esb.cook.proxy.service.AccLogService;
import com.lanyou.esb.cook.proxy.service.IfAccTimesService;
import com.lanyou.esb.cook.proxy.service.IfAlarmService;
import com.lanyou.esb.cook.proxy.service.IfAvgResTimesService;
import com.lanyou.esb.cook.proxy.service.ParamSetService;
import com.lanyou.esb.cook.proxy.service.PfThroughputService;
import com.lanyou.esb.cook.proxy.service.StoreForwardService;

/**
 * 推送消息至REDIS基础实现类
 * 
 * @author Davey.wu
 */
public class BasePushRedis implements PushRedis, Job {
	/** 推送日志消息到REDIS **/
	private AccLogService accLogService;
	/** 推送消息到REDIS **/
	private IfAccTimesService ifAccTimesService;
	/** 推送消息到REDIS **/
	private IfAlarmService ifAlarmService;
	/** 推送消息到REDIS **/
	private IfAvgResTimesService ifAvgResTimesService;
	/** 推送消息到REDIS **/
	private PfThroughputService pfThroughputService;
	/** 推送消息到REDIS **/
	private StoreForwardService storeForwardService;

	/** 参数配置服务接口 **/
	private ParamSetService paramSetService;

	private Scheduler scheduler;
	private String cron = "0 0/5 * * * ?";
	private String name;
	private String group;
	private Class jobClass;

	public BasePushRedis() {
		// 每5分钟执行一次
		this.cron = "0 0/5 * * * ?";
		this.name = "push message to redis";
		this.group = "push message to redis";
		this.jobClass = this.getClass();
	}

	@Override
	public void execute(JobExecutionContext context)
			throws JobExecutionException {
		BasePushRedis basePushRedis = (BasePushRedis) context.getJobDetail()
				.getJobDataMap().get("proxy");
		basePushRedis.push();
	}

	@Override
	public void push() {
		accLogService.getPush().push();
		ifAccTimesService.getPush().push();
		ifAlarmService.getPush().push();
		ifAvgResTimesService.getPush().push();
		pfThroughputService.getPush().push();
		storeForwardService.getPush().push();
	}

	@Override
	public void init() throws Exception {
		// 初始化参数
		//initParamSet();
		System.out.println("定时任务------推送消息到REDIS");
		try {
			scheduler = new StdSchedulerFactory().getScheduler();
			// 创建LzstoneTimeTask的定时任务
			// JobDetail jobDetail = new JobDetailImpl(name, group, jobClass);
			JobDetail jobDetail = JobBuilder.newJob(jobClass)
					.withIdentity(name, group).build();
			JobDataMap jobMap = jobDetail.getJobDataMap();
			jobMap.put("proxy", this);
			// 目标 创建任务计划
			CronTrigger trigger = new CronTriggerImpl(name, group, cron);
			// CronTrigger trigger =
			// TriggerBuilder.newTrigger().withIdentity(name,group).
			scheduler = new StdSchedulerFactory().getScheduler();
			scheduler.scheduleJob(jobDetail, trigger);
			scheduler.start();
		} catch (Exception e) {
			System.out.println("定时任务------推送消息到REDIS，失败！");
			throw e;
		}

	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

	private void initParamSet() {
		if (ParamFactory.haveParam()) {
			return;
		}
		// 加载参数配置
		paramSetService.init();
	}

	public ParamSetService getParamSetService() {
		return paramSetService;
	}

	public void setParamSetService(ParamSetService paramSetService) {
		this.paramSetService = paramSetService;
	}

	public AccLogService getAccLogService() {
		return accLogService;
	}

	public void setAccLogService(AccLogService accLogService) {
		this.accLogService = accLogService;
	}

	public IfAccTimesService getIfAccTimesService() {
		return ifAccTimesService;
	}

	public void setIfAccTimesService(IfAccTimesService ifAccTimesService) {
		this.ifAccTimesService = ifAccTimesService;
	}

	public IfAlarmService getIfAlarmService() {
		return ifAlarmService;
	}

	public void setIfAlarmService(IfAlarmService ifAlarmService) {
		this.ifAlarmService = ifAlarmService;
	}

	public IfAvgResTimesService getIfAvgResTimesService() {
		return ifAvgResTimesService;
	}

	public void setIfAvgResTimesService(
			IfAvgResTimesService ifAvgResTimesService) {
		this.ifAvgResTimesService = ifAvgResTimesService;
	}

	public PfThroughputService getPfThroughputService() {
		return pfThroughputService;
	}

	public void setPfThroughputService(PfThroughputService pfThroughputService) {
		this.pfThroughputService = pfThroughputService;
	}

	public StoreForwardService getStoreForwardService() {
		return storeForwardService;
	}

	public void setStoreForwardService(StoreForwardService storeForwardService) {
		this.storeForwardService = storeForwardService;
	}

}
